<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="迎着朝阳的博客" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    各种排序算法总结及java实现 |  迎着朝阳
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="https://dxysun.com/static/yan.png" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  
<script>
var _hmt = _hmt || [];
(function() {
	var hm = document.createElement("script");
	hm.src = "https://hm.baidu.com/hm.js?aa994a8d65700b8835787dd39d079d7e";
	var s = document.getElementsByTagName("script")[0]; 
	s.parentNode.insertBefore(hm, s);
})();
</script>


</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-allKindsOfSort"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  各种排序算法总结及java实现
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2018/07/10/allKindsOfSort/" class="article-date">
  <time datetime="2018-07-10T06:30:04.000Z" itemprop="datePublished">2018-07-10</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">3k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">14 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <p>总结一下各种排序算法</p>
<a id="more"></a>
<h2 id="时间复杂度"><a href="#时间复杂度" class="headerlink" title="时间复杂度"></a>时间复杂度</h2><p>常数时间的操作：一个操作如果和数据量没有关系，每次都是固定时间内完成的操作，叫做常数操作。<br>时间复杂度为一个算法流程中，常数操作数量的指标。常用O（读作big O）来表示。具体来说，在常数操作数量的表达式中，<br>只要高阶项，不要低阶项，也不要高阶项的系数，剩下的部分如果记为f(N)，那么时间复杂度为O(f(N))。<br>评价一个算法流程的好坏，先看时间复杂度的指标，然后再分析不同数据样本下的实际运行时间，也就是常数项时间。、</p>
<h2 id="空间复杂度"><a href="#空间复杂度" class="headerlink" title="空间复杂度"></a>空间复杂度</h2><p>为了支持算法的顺利运行，额外需要多少辅助空间</p>
<p>先定义一下基本的交换操作</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; i和j必须是不同位置的数（内存上是两个不同位置的数）</span><br><span class="line">public static void swap(int[] arr, int i, int j) &#123;</span><br><span class="line">    arr[i] &#x3D; arr[i] ^ arr[j];</span><br><span class="line">    arr[j] &#x3D; arr[i] ^ arr[j];</span><br><span class="line">    arr[i] &#x3D; arr[i] ^ arr[j];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">public static void swap(int[] arr, int i, int j) &#123;</span><br><span class="line">    int tmp &#x3D; arr[i];</span><br><span class="line">    arr[i] &#x3D; arr[j];</span><br><span class="line">    arr[j] &#x3D; tmp;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>^ 异或运算，有以下四个性质<br>N ^ 0 = N<br>N ^ N = 0<br>如果 a ^ b = c 则，a = b ^ c,b = a ^ c<br>异或运算满足交换律和结合律</p>
</blockquote>
<h2 id="递归行为和递归行为时间复杂度的估算"><a href="#递归行为和递归行为时间复杂度的估算" class="headerlink" title="递归行为和递归行为时间复杂度的估算"></a>递归行为和递归行为时间复杂度的估算</h2><p>一个递归行为的例子<br>master公式的使用<br>T(N) = a*T(N/b) + O(N^d)</p>
<p>1) log(b,a) &gt; d -&gt; 复杂度为O(N^log(b,a))<br>2) log(b,a) = d -&gt; 复杂度为O(N^d*logN)<br>3) log(b,a) &lt; d -&gt; 复杂度为O(N^d)</p>
<h2 id="对数器的概念和使用"><a href="#对数器的概念和使用" class="headerlink" title="对数器的概念和使用"></a>对数器的概念和使用</h2><p>0，有一个你想要测的方法a，<br>1，实现一个绝对正确但是复杂度不好的方法b，<br>2，实现一个随机样本产生器<br>3，实现比对的方法<br>4，把方法a和方法b比对很多次来验证方法a是否正确。<br>5，如果有一个样本使得比对出错，打印样本分析是哪个方法出错<br>6，当样本数量很多时比对测试依然正确，可以确定方法a已经正确。</p>
<h2 id="冒泡排序"><a href="#冒泡排序" class="headerlink" title="冒泡排序"></a>冒泡排序</h2><p>从第0个位置开始遍历，大的往后移，直到最大的数移动到最后一个位置。遍历完一遍之后，最后一个位置就不用管了，然后重复上面的步骤。<br>时间复杂度o(N^2)，额外空间复杂度o(1)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">public static void bubbleSort(int[] arr) &#123;</span><br><span class="line">    if (arr &#x3D;&#x3D; null || arr.length &lt; 2) &#123;</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    for (int e &#x3D; arr.length - 1; e &gt; 0; e--) &#123;</span><br><span class="line">        for (int i &#x3D; 0; i &lt; e; i++) &#123;</span><br><span class="line">            if (arr[i] &gt; arr[i + 1]) &#123;</span><br><span class="line">                swap(arr, i, i + 1);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="选择排序"><a href="#选择排序" class="headerlink" title="选择排序"></a>选择排序</h2><p>记第一个数为最小数，从第1个位置开始遍历，比较得到最小数的位置(index)，第1遍遍历完后，交换index与0位置的数字，接着从第2个位置重复上面的操作<br>时间复杂度o(N^2)，额外空间复杂度o(1)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">public static void selectSort(int[] arr) &#123;</span><br><span class="line">    if (arr &#x3D;&#x3D; null || arr.length &lt; 2) &#123;</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    for (int i &#x3D; 0; i &lt; arr.length - 1; i++) &#123;</span><br><span class="line">        int minIndex &#x3D; i;</span><br><span class="line">        for (int j &#x3D; i + 1; j &lt; arr.length; j++) &#123;</span><br><span class="line">            minIndex &#x3D; arr[j] &lt; arr[minIndex] ? j : minIndex;</span><br><span class="line">        &#125;</span><br><span class="line">        swap(arr, i, minIndex);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="插入排序"><a href="#插入排序" class="headerlink" title="插入排序"></a>插入排序</h2><p>假设数组前面基本有序，从1位置开始不断把数字插入到前面有序的数组中<br>时间复杂度o(N^2)，额外空间复杂度o(1)<br>最坏情况是o(N^2),最好情况(数组已经有序)是o(N)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">public static void insertionSort(int[] arr) &#123;</span><br><span class="line">    if (arr &#x3D;&#x3D; null || arr.length &lt; 2) &#123;</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    for (int i &#x3D; 1; i &lt; arr.length; i++) &#123;</span><br><span class="line">        for (int j &#x3D; i - 1; j &gt;&#x3D; 0 &amp;&amp; arr[j] &gt; arr[j + 1]; j--) &#123;</span><br><span class="line">            swap(arr, j, j + 1);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="归并排序"><a href="#归并排序" class="headerlink" title="归并排序"></a>归并排序</h2><p>递归的让左边部分有序，递归的让右边部分有序，再让整体有序。<br>时间复杂度o(N * log(N))，额外空间复杂度O(N)<br>时间省在哪：<br>比较行为没有浪费，之前的三种每遍历一次进行的比较，只确定了一个数的顺序，太多的比较被浪费<br>归并是组与组之间的比较，组内不管，比较行为得到了充分的利用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">public static void mergeSort(int[] arr) &#123;</span><br><span class="line">    if (arr &#x3D;&#x3D; null || arr.length &lt; 2) &#123;</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    mergeSort(arr, 0, arr.length - 1);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public static void mergeSort(int[] arr, int l, int r) &#123;</span><br><span class="line">    if (l &#x3D;&#x3D; r) &#123;    &#x2F;&#x2F;递归终止条件</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    int mid &#x3D; l + ((r - l) &gt;&gt; 1);</span><br><span class="line">    mergeSort(arr, l, mid);        &#x2F;&#x2F; o(N&#x2F;2)</span><br><span class="line">    mergeSort(arr, mid + 1, r);    &#x2F;&#x2F;o(N&#x2F;2)</span><br><span class="line">    merge(arr, l, mid, r);         &#x2F;&#x2F;o(N)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public static void merge(int[] arr, int l, int m, int r) &#123;</span><br><span class="line">    int[] help &#x3D; new int[r - l + 1];</span><br><span class="line">    int i &#x3D; 0;</span><br><span class="line">    int p1 &#x3D; l;</span><br><span class="line">    int p2 &#x3D; m + 1;</span><br><span class="line">    while (p1 &lt;&#x3D; m &amp;&amp; p2 &lt;&#x3D; r) &#123;</span><br><span class="line">        help[i++] &#x3D; arr[p1] &lt; arr[p2] ? arr[p1++] : arr[p2++];</span><br><span class="line">    &#125;</span><br><span class="line">    &#x2F;&#x2F;p1和p2一定有一个越界，有一个不越界</span><br><span class="line">    while (p1 &lt;&#x3D; m) &#123;</span><br><span class="line">        help[i++] &#x3D; arr[p1++];</span><br><span class="line">    &#125;</span><br><span class="line">    while (p2 &lt;&#x3D; r) &#123;</span><br><span class="line">        help[i++] &#x3D; arr[p2++];</span><br><span class="line">    &#125;</span><br><span class="line">    for (i &#x3D; 0; i &lt; help.length; i++) &#123;</span><br><span class="line">        arr[l + i] &#x3D; help[i];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="非递归"><a href="#非递归" class="headerlink" title="非递归"></a>非递归</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">public static void mergeSortNotDigui(int[] a) &#123;</span><br><span class="line">    if (a &#x3D;&#x3D; null || a.length &lt; 2)</span><br><span class="line">        return;</span><br><span class="line">    int interval &#x3D; 1;</span><br><span class="line">    while (interval &lt; a.length) &#123;</span><br><span class="line">        int i &#x3D; 0;</span><br><span class="line">        for (i &#x3D; 0; i &lt; a.length; i +&#x3D; (interval + 1)) &#123;</span><br><span class="line">            int r &#x3D; i + interval;</span><br><span class="line">            if (r &gt; a.length - 1)</span><br><span class="line">                r &#x3D; a.length - 1;</span><br><span class="line">            merge(a, i, (i + r) &#x2F; 2, r);</span><br><span class="line">        &#125;</span><br><span class="line">        interval &#x3D; 2 * interval + 1;     &#x2F;&#x2F; 需要设置好间隔</span><br><span class="line">    &#125;</span><br><span class="line">    merge(a, 0, interval &#x2F; 2, a.length - 1);   &#x2F;&#x2F;merge最后一次</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="小和问题和逆序对问题"><a href="#小和问题和逆序对问题" class="headerlink" title="小和问题和逆序对问题"></a>小和问题和逆序对问题</h3><h4 id="小和问题"><a href="#小和问题" class="headerlink" title="小和问题"></a>小和问题</h4><p>在一个数组中，每一个数左边比当前数小的数累加起来，叫做这个数组的小和。求一个数组<br>的小和。<br>例子：<br>[1,3,4,2,5]<br>1左边比1小的数，没有；<br>3左边比3小的数，1；<br>4左边比4小的数，1、3；<br>2左边比2小的数，1；<br>5左边比5小的数，1、3、4、2；<br>所以小和为1+1+3+1+1+3+4+2=16</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">public static int smallSum(int[] arr) &#123;</span><br><span class="line">    if (arr &#x3D;&#x3D; null || arr.length &lt; 2) &#123;</span><br><span class="line">        return 0;</span><br><span class="line">    &#125;</span><br><span class="line">    return mergeSort(arr, 0, arr.length - 1);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public static int mergeSort(int[] arr, int l, int r) &#123;</span><br><span class="line">    if (l &#x3D;&#x3D; r) &#123;</span><br><span class="line">        return 0;</span><br><span class="line">    &#125;</span><br><span class="line">    int mid &#x3D; l + ((r - l) &gt;&gt; 1);   &#x2F;&#x2F;防止溢出</span><br><span class="line">    return mergeSort(arr, l, mid) + mergeSort(arr, mid + 1, r) + merge(arr, l, mid, r);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public static int merge(int[] arr, int l, int m, int r) &#123;</span><br><span class="line">    int[] help &#x3D; new int[r - l + 1];</span><br><span class="line">    int i &#x3D; 0;</span><br><span class="line">    int p1 &#x3D; l;</span><br><span class="line">    int p2 &#x3D; m + 1;</span><br><span class="line">    int res &#x3D; 0;</span><br><span class="line">    while (p1 &lt;&#x3D; m &amp;&amp; p2 &lt;&#x3D; r) &#123;</span><br><span class="line">        res +&#x3D; arr[p1] &lt; arr[p2] ? (r - p2 + 1) * arr[p1] : 0;</span><br><span class="line">        help[i++] &#x3D; arr[p1] &lt; arr[p2] ? arr[p1++] : arr[p2++];</span><br><span class="line">    &#125;</span><br><span class="line">    while (p1 &lt;&#x3D; m) &#123;</span><br><span class="line">        help[i++] &#x3D; arr[p1++];</span><br><span class="line">    &#125;</span><br><span class="line">    while (p2 &lt;&#x3D; r) &#123;</span><br><span class="line">        help[i++] &#x3D; arr[p2++];</span><br><span class="line">    &#125;</span><br><span class="line">    for (i &#x3D; 0; i &lt; help.length; i++) &#123;</span><br><span class="line">        arr[l + i] &#x3D; help[i];</span><br><span class="line">    &#125;</span><br><span class="line">    return res;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="逆序对问题"><a href="#逆序对问题" class="headerlink" title="逆序对问题"></a>逆序对问题</h4><p>在一个数组中，左边的数如果比右边的数大，则折两个数构成一个逆序对，请打印所有逆序对</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">static int count &#x3D; 0;</span><br><span class="line">    public static void mergeSort(int[] a)&#123;</span><br><span class="line">        if(a &#x3D;&#x3D; null || a.length &lt; 2)</span><br><span class="line">            return;</span><br><span class="line"></span><br><span class="line">        mergeSort(a,0,a.length - 1);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private static void mergeSort(int[] a, int l, int r) &#123;</span><br><span class="line">        if(l &lt; r)&#123;</span><br><span class="line">            int mid &#x3D; l + ((r - l) &gt;&gt; 1);</span><br><span class="line">            mergeSort(a,l,mid);</span><br><span class="line">            mergeSort(a,mid + 1,r);</span><br><span class="line">            merge(a,l,mid,r);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private static void merge(int[] a, int l, int mid, int r) &#123;</span><br><span class="line">        int[] help &#x3D; new int[r - l + 1];</span><br><span class="line">        int i &#x3D; help.length - 1;   &#x2F;&#x2F;从大开始统计</span><br><span class="line">        int pl &#x3D; mid;</span><br><span class="line">        int pr &#x3D; r;</span><br><span class="line">        while (pl &gt;&#x3D; l &amp;&amp; pr &gt;&#x3D; mid + 1)&#123;</span><br><span class="line">            if(a[pl] &gt; a[pr])</span><br><span class="line">            &#123;</span><br><span class="line">                for(int j &#x3D; mid + 1;j &lt;&#x3D; pr;j ++)</span><br><span class="line">                &#123;</span><br><span class="line">                    System.out.println(a[pl] + &quot; &quot; + a[j]);</span><br><span class="line">                    count ++;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            help[i --] &#x3D; a[pl] &gt; a[pr] ? a[pl --] : a[pr --];</span><br><span class="line">        &#125;</span><br><span class="line">        while (pl &gt;&#x3D; l)&#123;</span><br><span class="line">            help[i --] &#x3D; a[pl --];</span><br><span class="line">        &#125;</span><br><span class="line">        while (pr &gt;&#x3D; mid  + 1)&#123;</span><br><span class="line">            help[i --] &#x3D; a[pr --];</span><br><span class="line">        &#125;</span><br><span class="line">        for (i &#x3D; 0;i &lt; help.length;i ++)&#123;</span><br><span class="line">            a[l + i] &#x3D; help[i];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>merge过程中要从大到小开始排序，不能从小开始排序，不然会重复统计，而且统计不全</p>
</blockquote>
<h2 id="快速排序"><a href="#快速排序" class="headerlink" title="快速排序"></a>快速排序</h2><h3 id="parition过程"><a href="#parition过程" class="headerlink" title="parition过程"></a>parition过程</h3><p>先随机选一个数和最后一个位置交换，在从左到右遍历，经过比较交换后，小于最后一个数的都放左边，大于最后一个数的都放在右边，等于的都在中间</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">public static int[] partition(int[] a,int l,int r)&#123;</span><br><span class="line"></span><br><span class="line">    int index &#x3D; l;</span><br><span class="line">    int less &#x3D; l - 1;</span><br><span class="line">    int more &#x3D; r;</span><br><span class="line">    while (index &lt; more)&#123;</span><br><span class="line">        if(a[index] &lt; a[r])&#123;</span><br><span class="line">            swap(a,++ less, index ++);</span><br><span class="line">        &#125;</span><br><span class="line">        else if(a[index] &gt; a[r])&#123;</span><br><span class="line">            swap(a,-- more,index);</span><br><span class="line">        &#125;</span><br><span class="line">        else &#123;</span><br><span class="line">            index ++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    swap(a,more,r);</span><br><span class="line"></span><br><span class="line">    return new int[]&#123;less + 1,more&#125;;  &#x2F;&#x2F;返回中间相等数的第一个下标和最后一个下标</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="快排"><a href="#快排" class="headerlink" title="快排"></a>快排</h3><p>递归重复上面过程<br>最好情况下时间复杂度o(N * log(N))，额外空间复杂度o(log(N))<br>最坏情况下时间复杂度o(N ^ 2)，额外空间复杂度o(N)<br>但由于中间的数是随机选择的，所以总体期望是<br>时间复杂度o(N * log(N))，额外空间复杂度o(log(N))</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">public static void quickSort(int[] a) &#123;</span><br><span class="line">    if (a &#x3D;&#x3D; null || a.length &lt; 2)</span><br><span class="line">        return;</span><br><span class="line">    quickSort(a, 0, a.length - 1);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public static void quickSort(int[] a,int l,int r)&#123;</span><br><span class="line">    if(l &lt; r)&#123;</span><br><span class="line">        swap(a,l + (int)(Math.random() * (r - l + 1)),r);</span><br><span class="line">        int[] res &#x3D; partition(a,l,r);</span><br><span class="line">        quickSort(a,l,res[0] - 1);</span><br><span class="line">        quickSort(a,res[1] + 1,r);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="堆排序"><a href="#堆排序" class="headerlink" title="堆排序"></a>堆排序</h2><p>时间复杂度O(N * logN)，额外空间复杂度O(1)</p>
<h3 id="堆结构"><a href="#堆结构" class="headerlink" title="堆结构"></a>堆结构</h3><p>堆是一个完全二叉树<br>从0位置开始的一段连续数组可以对应一个完全二叉树<br>即当前位置为i，左孩子为2 * i + 1，右孩子为2 * i + 2，父节点(i &gt; 0)位置为(i - 1) / 2<br>堆可分为大顶堆和小顶堆<br>大顶堆，根节点为最大值<br>小顶堆，根节点为最小值</p>
<p>堆有两种调整<br>插入数据，向上调整(建立堆，时间复杂度为o(N))<br>移除堆顶数据，向下调整(时间复杂度O(N * logN))</p>
<p>节点i的高度为log(i)</p>
<h3 id="排序"><a href="#排序" class="headerlink" title="排序"></a>排序</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">public static void heapSort(int[] a) &#123;</span><br><span class="line">    if (a &#x3D;&#x3D; null || a.length &lt; 2)</span><br><span class="line">        return;</span><br><span class="line"></span><br><span class="line">    for (int i &#x3D; 0; i &lt; a.length; i++) &#123;</span><br><span class="line">        heapInsert(a, i);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    int size &#x3D; a.length;</span><br><span class="line">    while (size &gt; 0) &#123;</span><br><span class="line">        swap(a, 0, --size);</span><br><span class="line">        heapify(a, 0, size);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public static void heapInsert(int[] a, int i) &#123;</span><br><span class="line">    while (a[(i - 1) &#x2F; 2] &lt; a[i]) &#123;</span><br><span class="line">        swap(a, (i - 1) &#x2F; 2, i);</span><br><span class="line">        i &#x3D; (i - 1) &#x2F; 2;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public static void heapify(int[] a, int i, int size) &#123;</span><br><span class="line"></span><br><span class="line">    int l &#x3D; 2 * i + 1;</span><br><span class="line">    while (l &lt; size) &#123;</span><br><span class="line">        int bigger_pos &#x3D; l + 1 &lt; size &amp;&amp; a[l + 1] &gt; a[l] ? l + 1 : l;</span><br><span class="line"></span><br><span class="line">        if (a[i] &lt; a[bigger_pos]) &#123;</span><br><span class="line">            swap(a, i, bigger_pos);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            break;</span><br><span class="line">        &#125;</span><br><span class="line">        i &#x3D; bigger_pos;</span><br><span class="line">        l &#x3D; 2 * i + 1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="排序算法稳定性比较"><a href="#排序算法稳定性比较" class="headerlink" title="排序算法稳定性比较"></a>排序算法稳定性比较</h2><table>
<thead>
<tr>
<th align="left">排序算法</th>
<th align="left">稳定性</th>
<th align="left">时间复杂度</th>
<th align="left">额外空间复杂度</th>
</tr>
</thead>
<tbody><tr>
<td align="left">冒泡排序</td>
<td align="left">稳定</td>
<td align="left">o(N^2)</td>
<td align="left">o(1)</td>
</tr>
<tr>
<td align="left">选择排序</td>
<td align="left">不稳定</td>
<td align="left">o(N^2)</td>
<td align="left">o(1)</td>
</tr>
<tr>
<td align="left">插入排序</td>
<td align="left">稳定</td>
<td align="left">o(N^2)</td>
<td align="left">o(1)</td>
</tr>
<tr>
<td align="left">归并排序</td>
<td align="left">稳定</td>
<td align="left">o(N * log(N))</td>
<td align="left">o(N)</td>
</tr>
<tr>
<td align="left">快速排序</td>
<td align="left">不稳定</td>
<td align="left">o(N * log(N))</td>
<td align="left">o(log(N))</td>
</tr>
<tr>
<td align="left">堆排序</td>
<td align="left">不稳定</td>
<td align="left">o(N * log(N))</td>
<td align="left">o(1)</td>
</tr>
</tbody></table>
<blockquote>
<p>以上都是基于比较的排序<br>额外知识<br>归并排序额外空间复杂度可变成o(1)，即归并排序的内部缓存法，但会变成不稳定的<br>快速排序可以做到稳定性问题，额外空间复杂度会变成o(N)，但是非常难，可以搜”01 stable sort”</p>
</blockquote>
<h2 id="额外的排序"><a href="#额外的排序" class="headerlink" title="额外的排序"></a>额外的排序</h2><h3 id="希尔排序"><a href="#希尔排序" class="headerlink" title="希尔排序"></a>希尔排序</h3><p>改进的插入排序，即逐渐调整步长的插入排序</p>
<h3 id="非基于比较的排序"><a href="#非基于比较的排序" class="headerlink" title="非基于比较的排序"></a>非基于比较的排序</h3><h4 id="桶排序"><a href="#桶排序" class="headerlink" title="桶排序"></a>桶排序</h4><p>数组中的数字在一定范围内，一个时间复杂度为o(N)的排序<br>桶就是容器，可以是数组，栈，队列等<br>示例<br>对一个大小在0-60之间的数组排序，申请一个数组大小为61的数组，初始化为0<br>对要排序的数组进行遍历，用大小为61的数组记录数字出现的次数即可，遍历完后，打印大小为61的数组(即桶)，大于0的就打印</p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://dxysun.com/2018/07/10/allKindsOfSort/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/" rel="tag">java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E7%AE%97%E6%B3%95/" rel="tag">算法</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2018/07/14/javaForConcurrent2/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            java备忘录之并发（二）
          
        </div>
      </a>
    
    
      <a href="/2018/07/10/dockerForCommmand/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">常用docker命令</div>
      </a>
    
  </nav>

  
   
  
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2024
        <i class="ri-heart-fill heart_icon"></i> dxysun
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
        <li>
          <a href="https://beian.miit.gov.cn" target="_black" rel="nofollow">豫ICP备17012675号-1</a>
        </li>
        
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="https://dxysun.com/static/logo.png" alt="迎着朝阳"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/photos">相册</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://tu.dxysun.com/alipay-20201219151322.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://tu.dxysun.com/weixin-20201219151346.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
  var ayerConfig = {
    mathjax: true
  }
</script>

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


<script src="/js/dz.js"></script>



    
  </div>
</body>

</html>